package com.qq.xrx1002.androidgame;
import android.util.SparseArray;

import org.cocos2d.actions.CCScheduler;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by Alyson on 2018/11/17.
 */
public class CombatLine {
    private SparseArray<Plant> plants;
    private ArrayList<Zombie> zombies;//数组列表存僵尸
    private ArrayList<ShooterPlant>shooterPlants;//数组列表存放植物子弹
    public CombatLine() {
        plants = new SparseArray<>();
        zombies=new ArrayList<>();
        shooterPlants=new ArrayList<>();
        CCScheduler.sharedScheduler().schedule("attackPlant",this,1,
                false);//攻击植物
        CCScheduler.sharedScheduler().schedule("attackZombie",this,1,
                false);//攻击僵尸
        CCScheduler.sharedScheduler().schedule("bulletHurtCompute",this,
                0.2f,false);//添加定时器用来子弹的伤害计算
    }
    public boolean isContainPlant(int col) {
        if (plants.get(col)!=null){
            return true;
        }
        return false;
    }
    public void addPlant(int col, Plant plant) {
        plants.put(col,plant);
        if (plant instanceof ShooterPlant){
            shooterPlants.add((ShooterPlant) plant);
        }
    }
    public void addZombie(Zombie zombie){//加入僵尸
        zombies.add(zombie);
    }
    public void attackPlant(float t){//攻击植物的方法
        if (zombies.size()!=0&&plants.size()!=0){//zombies.size()!=0:说明里面存有植物；plants.size()!=0:说明里面有僵尸
            for (Zombie zombie:zombies){
                int col=(int)(zombie.getPosition().x-280)/105;//zombie.getPosition().x:得到僵尸的位置，相当于地图的位置，则需要减去105；105:每列大概为105
                if (isContainPlant(col)){//是否包含植物
                    switch (zombie.getState()){//则得到僵尸的状态
                        case MOVE:
                            zombie.stopAllActions();//僵尸停下所有状态
                            zombie.attack();//则僵尸攻击植物
                            break;
                        case ATTACK:
                            Plant plant=plants.get(col);//取出植物
                            plant.hurtCompute(zombie.getAttack());//hurtCompute:计算伤害值
                            if (plant.getHP()==0){
                                plants.remove(col);
                                plant.removeSelf();//plants.remove(col);plant.removeSelf();：移除植物
                                zombie.stopAllActions();
                                zombie.move();//zombie.stopAllActions();zombie.move();：则停下所有攻击动作改为移动
                            }
                            break;
                    }
                }else if (zombie.getState()==Zombie.State.ATTACK){
                    zombie.stopAllActions();
                    zombie.move();
                }
            }
        }
    }
    public void attackZombie(float t){
        if (!shooterPlants.isEmpty()){
            for (ShooterPlant shooterPlant:shooterPlants){
                if (zombies.isEmpty()){
                    shooterPlant.stopAttackZombie();
                }else {
                    shooterPlant.attackZombie();
                }
            }
        }
    }
    public void bulletHurtCompute(float t){
        if (!shooterPlants.isEmpty()&&!zombies.isEmpty()){
            for (ShooterPlant shooterPlant:shooterPlants){
                for (Bullet bullet:shooterPlant.getBullets()){
                    Iterator<Zombie>iterator=zombies.iterator();
                    while (iterator.hasNext()){
                        Zombie zombie=iterator.next();
                        if (bullet.getVisible()&&bullet.getPosition().x>zombie.getPosition().x-20
                                &&bullet.getPosition().x<zombie.getPosition().x+20){
                            bullet.showBulletBlast(zombie);
                            bullet.setVisible(false);
                            zombie.hurtCompute(bullet.getAttack());
                            if (zombie.getHP()==0){
                                zombie.removeSelf();
                                iterator.remove();
                            }
                        }
                    }
                }
            }
        }
    }
}
